<?php
namespace jianan\controllers;

use Yii;
use yii\web\Controller;
use yii\data\Pagination;
use jianan\models\Room;
use jianan\models\Floor;
use jianan\models\Gree;
use jianan\models\Master;
header("Content-type:text/html;charset=utf-8");

class GreeController extends Controller {

	public function actionList() {
		$model = new Gree;
		$query = Gree::find();
		$countQuery = clone $query;
		$pages = new Pagination(['totalCount' => $countQuery->count(),'pageSize' => 20]);
		if(!empty($_POST['Gree'])){

			$model = $query->where($this->term($_POST['Gree']))->offset($pages->offset)->limit($pages->limit)->orderBy(['addTime'=> SORT_DESC])->orderBy(['id'=> SORT_DESC])->all();
		} else {
			$model = $query->offset($pages->offset)->limit($pages->limit)->orderBy(['addTime'=> SORT_DESC])->orderBy(['id'=> SORT_DESC])->all();
		}
		foreach ($model as $v) {
			$saleman = Master::findOne($v['saleman']);
			$status = Room::findOne($v['rid']);
			$v['status'] = $status['status'];
			$v['saleman'] = $saleman['masterName'];
			$data = json_decode($v['data']);
			$v['rid'] = $data->floorname.'-'.$data->unitName.'-'.$data->roomId;
		}
		
		return $this->render('list',['model'=>$model,'pages'=>$pages]);
	}

	public function term($addtime){
		$term = strtotime($addtime['addtime']);
		if(isset($term) && $term>0){

			$start = mktime(0,0,0,date("m",$term),date("d",$term),date("Y",$term));

			$end = mktime(23,59,59,date("m",$term),date("d",$term),date("Y",$term));

			$re = [
				'AND',
				['>','addTime',$start],
				['<','addTime',$end]

			];
		}
		return $re;

	}
}